package com.cloud.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

public class InternalPage extends AnyPage {


    //Constructor
    public InternalPage(PageManager pages) {
        super(pages);
    }


    //Page elements
    @FindBy(xpath = "/html/body/div[3]/div[2]/div[1]/div[2]/ul/li/span/a")
    private WebElement userMenu;

    @FindBy(xpath = "/html/body/div[3]/div[2]/div[1]/div[2]/ul/li/ul/li[2]/a")
    private WebElement myProfileMenuItem;

    @FindBy(xpath = "/html/body/div[3]/div[2]/div[1]/div[2]/ul/li/ul/li[6]/a")
    private WebElement signOut;


    //Methods
    public InternalPage ensurePageLoaded() {
        wait.until(visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[2]/div[1]/div[3]/ul/li[2]/a")));
        return this;
    }

    public boolean isLoaded() {
        try {
            wait.until(presenceOfElementLocated(By.xpath("/html/body/div[3]/div[2]/div[1]/div[3]/ul/li[2]/a")));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public InternalPage clickUserMenu() {
        userMenu.click();
        return this;
    }

    public MyProfilePage clickMyProfile() {
        myProfileMenuItem.click();
        return pages.myProfilePage;
    }

    public LoginPage logout() {
        Actions builder = new Actions(driver);
        builder.moveToElement(userMenu).perform();
        builder.moveToElement(signOut).perform();
        signOut.click();
        LoginPage loginPage = pages.loginPage.ensurePageLoaded();
        return loginPage;
    }

    public MyProfilePage openMyProfile() {
        Actions builder = new Actions(driver);
        builder.moveToElement(userMenu).perform();
        builder.moveToElement(myProfileMenuItem).perform();
        myProfileMenuItem.click();
        MyProfilePage myProfilePage = pages.myProfilePage.ensurePageLoaded();
        return myProfilePage;
    }
}
